#!/bin/bash

## Wrapper around infertop. To build infertop, run `make toplevel`.

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# where to find toplevel.bc, can be overridden
BUILD_DIR=${BUILD_DIR:-"$SCRIPT_DIR"/../infer/_build/default/src}
BIN_DIR="$SCRIPT_DIR/../infer/bin"
INCLUDE_FLAGS=$(find "$BUILD_DIR" -type d -name '*.objs' -exec printf -- '-I {}/byte\n' \;)

# The -init option is effective only in interactive mode.
# In batch mode, scripts need to import toplevel_init themselves.
# It can be done by adding #use "toplevel_init";; to the beginning
# of a script.
# NOTE: $SCRIPT_DIR is added search path for batch scripts
#       so they can be located anywhere and still find toplevel_init
#       file. In interactive mode $SCRIPT_DIR isn't needed

set -x
$BIN_DIR/infertop.bc.exe \
  -init "$SCRIPT_DIR"/toplevel_init \
  $INCLUDE_FLAGS \
  -I "$SCRIPT_DIR" \
  "$@"
